package com.sensorfriendly.ui;

import java.util.List;

import com.sensorfriendly.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

/**
 * 
 * Adapter for hosting icons for the EditsScenarioDialog's gridview.
 *
 */
public class IconAdapter extends BaseAdapter {
    private Context mContext;
    private List<Bitmap> icons;
    private List<String> iconNames;
    private int selectedIconIndex;
    
    public IconAdapter(Context c, List<Bitmap> icons, List<String> iconNames) {
        mContext = c;
        this.iconNames = iconNames;
        this.icons = icons;
        selectedIconIndex = -1;
    }

    public int getCount() {
        return icons.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return position;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  
        	// if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(80, 80));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(5, 5, 5, 5);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageBitmap(icons.get(position));
        imageView.setTag(iconNames.get(position));
        
        // the icon selected by the user has a bounding box
        if(position == selectedIconIndex){
        	imageView.setBackgroundResource(R.drawable.selected_icon_border);
        }
        return imageView;
    }

	public int getSelectedIconIndex() {
		return selectedIconIndex;
	}

	public void setSelectedIconIndex(int selectedIconIndex) {
		this.selectedIconIndex = selectedIconIndex;
	}
    
    

}